<?php
namespace Admin\Controller;
use Think\Controller;
class CategoryController extends CommonController {
    var $category;
    function __construct()
    {
        parent::__construct();
        $this->category=M('category');
        $categories=$this->category->select();
        $this->assign('categories',$categories);

    }

    function index(){
        $categories=$this->category->where("parent_id=0")->order('sort_order')->select();
        //echo $this->category->getLastSql();exit;
        foreach ($categories as &$v){
            $id=$v["id"];
            $children=$this->category->where("parent_id=$id")->select();
            //dump($children);exit;
            $v["children"]=$children;
        }
        //dump($categories);exit;

        $this->assign('categories',$categories);
        $this->display();
    }

    function add(){
        $categories=$this->category->where("parent_id=0")->order('sort_order')->select();
        $this->assign('categories',$categories);
        //dump($categories);exit;

        $this->display();
    }

    function store()
    {
        //dump($_POST);
        ///*
        //dump($_POST);exit;
        $this->category->create();
        $this->category->add();
        $this->redirect('index');
        //*/
    }

    function edit(){
        $id=I("get.id");
        $c=$this->category->find($id);
        $categories=$this->category->where("parent_id=0")->order('sort_order')->select();
        $this->assign('categories',$categories);
        $this->assign("c",$c);
        //dump($c);exit;
        $this->display();
    }

    function update()
    {

        $this->category->create();
        $this->category->save();

       $this->success('修改成功',U('index'));
    }
    function destroy(){
        $id=I("get.id");
        $this->category->where("id=$id")->delete();
        $this->redirect('index');
    }
    function sort_link()
    {
        $ids=I("post.ids");
        $sort_order = I("post.sort_order");


        foreach($ids as $key=>$i) {
            
            $this->category->where("id=$i")->setField('sort_order', $sort_order["$key"]);

        }
        $this->redirect('index');
    }
    function category_content(){
        //dump($_POST);
        $this->category->create();
        $this->category->add();
    }

}

